<template>
    <div class="table_wrapper">
        <div class="my_table_ctr">
            <div class="my_table_ctr_left">
                <el-button type="primary" @click="add" size="mini">添加</el-button>
            </div>
            <div class="my_table_ctr_right">
                <el-button type="info" icon="el-icon-refresh" @click="refresh" size="mini"></el-button>
            </div>
        </div>
        <div class="my_table_data">
            <el-table  :data="quartzs"
                       size="mini"
                       :border="true"
                       :stripe="true"
                       :highlight-current-row="true"
                       @selection-change="selectChange"
                       ref="table"
                       @sort-change="sortChange"
                       v-loading="tableLoading">

                <el-table-column
                        type="index">
                </el-table-column>
                <el-table-column prop="quartzName" label="定时器名称">
                </el-table-column>
                <el-table-column prop="cron" label="cron表达式">
                </el-table-column>
                <el-table-column prop="quartzDesc" label="定时器描述">
                </el-table-column>
                <el-table-column prop="quartzState" label="状态">
                    <template slot-scope="scope">
                        <el-tag type="primary" size="mini"  v-if="scope.row.quartzState===0">启动</el-tag>
                        <el-tag type="danger" size="mini"  v-if="scope.row.quartzState===1">暂停</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="createdTime" label="创建时间">
                </el-table-column>

                <el-table-column label="操作" width="190px" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="primary"
                                @click="updateState(scope.row)" >{{scope.row.quartzState===0?'停止':'启动'}}</el-button>
                        <el-button
                                size="mini"
                                plain
                                @click="handleEdit(scope.row)" icon="fa fa-edit "></el-button>
                        <el-button
                                size="mini"
                                plain
                                type="danger"
                                @click="deleteQuartz(scope.row)"
                                icon="fa fa-trash"></el-button>


                    </template>
                </el-table-column>
            </el-table>
        </div>
        <save ref="save" @refresh="init"></save>
    </div>
</template>

<script>
    import curdMixins from '../../../common/mixins/curdMixins'
    import save from './save/save'
    import {selectQuartzList,deleteById,updateQuartzState} from "../../../api/base/quartz";

    export default {
        name: "quartz",
        mixins:[curdMixins],
        components:{
            save,
        },
        data(){
            return{
                quartzs:[],
                tableLoading:false,
            }
        },
        created(){
            this.init();
        },
        methods:{
            init(){
                this.tableLoading=true;
                selectQuartzList().then(res=>{
                    if(res.status===0){
                        this.quartzs=res.data;
                    }
                    this.tableLoading=false;
                })
            },
            deleteQuartz(data) {
                this.$confirm('确定删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteById(data).then(res=>{
                        if(res.status===0){
                            this.$notify({
                                title: '删除成功',
                                type: 'success',
                                duration: 1500
                            });
                            this.init();
                        }else {
                            this.$message.error(res.msg);
                        }
                    })
                }).catch(() => {

                });
            },
            updateState(data){
                data=JSON.parse(JSON.stringify(data));
                if(data.quartzState){
                    data.quartzState=0;
                }else {
                    data.quartzState=1;
                }
                updateQuartzState(data).then(res=>{
                    if(res.status===0){
                        this.$message.success("修改成功");
                        this.init();
                    }else {
                        this.$message.error(res.msg);
                    }
                })
            }
        }

    }
</script>

<style scoped lang="sass">
    @import "../../../styles/table"
</style>
